
cd ".\data\clean\reliabcomp"
 

* Re-Construct Reliability Data

**** Baseline      ***

****   Rolling statistics. So use the prior X number of days to form the median and 80th quantile. 
*local nlist 15 30 60 90
local nlist 30
forval i=1/1{
	local j: word `i' of `nlist'
	**ML data
	use ".\data\clean\MLspeeds_new5mindata", clear
	/* g month=month(date)
	g year=year(date)
	g fwy=10 */
	capture g dow=dow(date)
	g weekend=cond(dow==0 |dow==6,1,0)
	drop if weekend==1
	qui sum date
	local min=`r(min)'
	replace date=date-`min'
	local max=`r(max)'
	qui levelsof date, l(time)
	local first=1
	forval t=`j'/`max'{
		preserve
		qui keep if date<`t' & date>`t'-`j'
		local n=_N
		if `n'>0{
			collapse (p50) p50_speedML=speed (p25) p25_speedM=speed (p20) p20_speedML=speed (p10) p10_speedML=speed (sd) sd_speedML=speed, by(hour fwy dir)
			g date=`t'+`min'
			if `first'==1{ 
				save ML_hourbuffer, replace 
				}
			else if `first'==0{ 
				append using ML_hourbuffer
				qui save ML_hourbuffer, replace
				}
			}
		restore
		local first=0
		di "`t' of `max'"
		}
		

	**HV
	use ".\data\clean\HV_reliab_dataset", clear
	keep if fwy==10&dir=="W"
	capture g dow=dow(date)
	g weekend=cond(dow==0 |dow==6,1,0)
	drop if weekend==1
	qui sum date
	local min=`r(min)'
	replace date=date-`min'
	local max=`r(max)'
	qui levelsof date, l(time)
	local first=1
	forval t=`j'/`max'{
		preserve
		qui keep if date<`t' & date>`t'-`j'
		local n=_N
		if `n'>0{
			collapse (p50) p50_speedHV=speed (p25) p25_speedHV=speed (p20) p20_speedHV=speed (p10) p10_speedHV=speed (sd) sd_speedHV=speed, by(hour fwy dir)
			g date=`t'+`min'
			if `first'==1{ 
				save HV_hourbuffer, replace 
				}
			else if `first'==0{ 
				append using HV_hourbuffer
				qui save HV_hourbuffer, replace
				}
			}
		restore
		local first=0
		}
	use HV_hourbuffer, clear
	merge 1:1 date hour  using ML_hourbuffer, nogen 
	 g year=year(date)
	drop if year==2014
	g  month=month(date)
	g day=day(date)
	save HV_ML_reliab_`j'days, replace

	}
	
